package com.codedancer.campus.manager.api.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.anno.PassToken;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.admin.ro.AdminAddOrUpdateRo;
import com.codedancer.campus.manager.api.admin.ro.AdminLoginRo;
import com.codedancer.campus.manager.api.admin.ro.AdminPageSearchRo;
import com.codedancer.campus.manager.service.IAdminService;
import com.codedancer.campus.manager.service.bo.admin.AdminAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.admin.AdminLoginBo;
import com.codedancer.campus.manager.service.bo.admin.AdminPageSearchBo;
import com.codedancer.campus.manager.service.vo.admin.AdminInfoVo;
import com.codedancer.campus.manager.service.vo.admin.AdminPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import javax.security.auth.message.AuthException;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/admin")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "管理员Api", description = "管理员Api", tags = "管理员Api")
public class AdminApi extends BaseApi {

    @Autowired
    private IAdminService adminService;

    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @PassToken
    public ApiResponse<String> login(
        @RequestBody @Valid @ApiParam(value = "管理员登录Ro", required = true)
        AdminLoginRo adminLoginRo) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return success(
            adminService.login(BeanUtil.copyProperties(adminLoginRo, AdminLoginBo.class)));
    }

    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    public ApiResponse<AdminInfoVo> currentUserInfo() throws AuthException {
        return success(adminService.currentUserInfo(currentLoginUuid()));
    }

    @GetMapping("/page")
    @ApiOperation("分页查询管理员")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<IPage<AdminPageVo>> page(
        @ApiParam(value = "管理员分页查询Ro", required = false)
        AdminPageSearchRo adminPageSearchRo) {
        AdminPageSearchBo adminPageSearchBo = BeanUtil.copyProperties(adminPageSearchRo,
            AdminPageSearchBo.class);
        return success(adminService.page(adminPageSearchBo));
    }

    @PostMapping("/add")
    @ApiOperation("添加管理员")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> add(
        @RequestBody @Valid @ApiParam(value = "添加管理员Ro", required = true)
        AdminAddOrUpdateRo adminAddOrUpdateRo) {
        AdminAddOrUpdateBo adminAddOrUpdateBo = BeanUtil.copyProperties(adminAddOrUpdateRo,
            AdminAddOrUpdateBo.class);
        adminService.add(adminAddOrUpdateBo);
        return success();
    }

    @PutMapping("/update/{uuid}")
    @ApiOperation("修改管理员")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> update(
        @RequestBody @Valid @ApiParam(value = "修改管理员Ro", required = true)
        AdminAddOrUpdateRo adminAddOrUpdateRo,
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "管理员uuid", required = true) String uuid) {
        AdminAddOrUpdateBo adminAddOrUpdateBo = BeanUtil.copyProperties(adminAddOrUpdateRo,
            AdminAddOrUpdateBo.class);
        adminAddOrUpdateBo.setUuid(uuid);
        adminService.update(adminAddOrUpdateBo);
        return success();
    }

    @PutMapping("/enable/{uuid}")
    @ApiOperation("启用管理员")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> enable(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "管理员uuid", required = true) String uuid) {
        adminService.enable(uuid, currentLoginUuid());
        return success();
    }

    @PutMapping("/disable/{uuid}")
    @ApiOperation("禁用管理员")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> disable(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "管理员uuid", required = true) String uuid) {
        adminService.disable(uuid, currentLoginUuid());
        return success();
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除管理员")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "管理员uuid", required = true) String uuid) {
        adminService.delete(uuid);
        return success();
    }
}
